{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is the demo code for running velocity esitmation evaluation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from evaluate.velocity import VeloEval\n",
    "import copy\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We first create a file list containing all the ground truth annotation files. Then we load them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_path = '/mnt/truenas/scratch/chenyangli/benchmark/v2/clips/'\n",
    "folder_path = os.listdir(dataset_path)\n",
    "annotations = [os.path.join(dataset_path, x, 'annotation.json') for x in folder_path]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finished loading 246 annotations.\n"
     ]
    }
   ],
   "source": [
    "gt = VeloEval.load_annotation(annotations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should load your prediction in a similar way we load ground truth above in the following section. Since this is just a demonstration, we create a fake prediction by adding some random number to the ground truth. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pred = copy.deepcopy(gt)\n",
    "for idx in range(len(pred)):\n",
    "    for j in range(len(pred[idx])):\n",
    "        pred[idx][j][\"velocity\"][0] += np.random.normal(0, 0.5)\n",
    "        pred[idx][j][\"velocity\"][1] += np.random.normal(0, 0.5)\n",
    "        pred[idx][j][\"position\"][0] += np.random.normal(0, 0.5)\n",
    "        pred[idx][j][\"position\"][1] += np.random.normal(0, 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Velocity Estimation error (Near): 0.56655\n",
      "Velocity Estimation error (Medium): 0.47895\n",
      "Velocity Estimation error (Far): 0.47028\n",
      "Velocity Estimation error total:  0.505260\n",
      "Position Estimation error (Near): 0.48858\n",
      "Position Estimation error (Medium): 0.53651\n",
      "Position Estimation error (Far): 0.57087\n",
      "Position Estimation error total: 0.53199\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(0.50526015569467364, 0.53198862232833877)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "VeloEval.accuracy(pred, gt)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
